//
//  EditCodeFormatViewController.swift
//  MyColorPicker
//
//  Created by 火星人 on 2018/9/22.
//  Copyright © 2018年 火星人. All rights reserved.
//

import Cocoa

class EditCodeFormatViewController: NSViewController, NSTextFieldDelegate {
   
    @IBOutlet weak var formatTextField: NSTextField!
    @IBOutlet weak var argsTextField: NSTextField!
    @IBOutlet weak var testTextField: NSTextField!
    
    var key = "nil"
    var formatString = ""
    var paramString = ""
    
    var didSetFormat: (() -> Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        formatTextField.delegate = self
        argsTextField.delegate = self
    }
    
    override func viewDidAppear() {
        
        if formatString == "", let value = (userDefaults.object(forKey: key) as? [String]) {
            formatString = value[0]
            for i in 1 ... value.count - 1 {
                paramString += value[i] + ","
            }
            if paramString.count > 0 {
                paramString = String(paramString[..<paramString.index(paramString.startIndex, offsetBy: paramString.count - 1)])
            }
            formatTextField.stringValue = formatString
            argsTextField.stringValue = paramString
            testAction()
        }
        
        formatTextField.isEnabled = true
        argsTextField.isEnabled = true
        
        testTextField.textColor = NSColor(red: 1.00, green: 0.52, blue: 0.00, alpha: 1.00)
    }
    
    @IBAction func applyAction(_ sender: Any) {
        
        if self.testTextField.stringValue != "Format Error!!!" {
            userDefaults.set(genArray(), forKey: key)
            let alert = NSAlert()
            alert.messageText = "Success !"
            alert.icon = NSImage(named: "operate_ok")
            
            weak var wself = self
            alert.beginSheetModal(for: self.view.window!) { (resp) in
                wself?.view.window?.close()
                wself?.didSetFormat?()
            }
        }
        else{
            let alert = NSAlert(error: NSError(domain: "Format Error!!!", code: 0, userInfo: nil))
            alert.beginSheetModal(for: self.view.window!)
        }
    }
    @IBAction func cancelAction(_ sender: Any) {
        self.view.window?.close()
    }
    private func genArray() -> [String]{
        var format = [self.formatTextField.stringValue]
        let array = self.argsTextField.stringValue.split(separator: ",")
        for i in array {
            format.append(String(i).trimmingCharacters(in: .whitespaces))
        }
        return format
    }
    private func testAction() {
        let format = genArray()
        let value = CodeFormatUtils.genCodeWithFormat(format: format, colorComponents: [CGFloat(20/255.0), CGFloat(30/255.0), CGFloat(40/255.0)])
        if value != "" {
            self.testTextField.stringValue = value
        }
        else {
            self.testTextField.stringValue = "Format Error!!!"
        }
    }
    
    func controlTextDidChange(_ obj: Notification) {
        self.testAction()
    }
    
}
